package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;

	@GetMapping("/findUserTrackPaidList/{trackId}")
	@Operation(summary = "获取用户声音分集购买支付列表")
	@TsLogin
	public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId) {
		Long userId = AuthContextHolder.getUserId();
		List<Map<String, Object>> list = trackInfoService.findUserTrackPaidList(trackId, userId);
		return Result.ok(list);
	}

	@GetMapping("/findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
	@Operation(summary = "根据专辑Id获取专辑声音列表")
	@TsLogin(required = false)
	public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId,
									 @PathVariable Long pageNo,
									 @PathVariable Long pageSize) {
		//获取用户id
		Long userId = AuthContextHolder.getUserId();
		Page<AlbumTrackListVo> page = new Page<>(pageNo, pageSize);
		trackInfoService.findAlbumTrackPage(page, albumId, userId);
		return Result.ok(page);
	}

	@PostMapping("/uploadTrack")
	@Operation(summary = "上传声音文件到云点播")
	public Result<Map<String,Object>> uploadTrack(MultipartFile file) {
		Map<String,Object> map = vodService.uploadTrack(file);
		return Result.ok(map);
	}

	@PostMapping("/saveTrackInfo")
	@Operation(summary = "保存声音信息")
	@TsLogin
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
		long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		trackInfoService.saveTrackInfo(trackInfoVo,userId);
		return Result.ok();
	}

	@PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
	@Operation(summary = "分页查询用户声音信息")
	@TsLogin
	public Result findUserTrackPage(@PathVariable Long pageNo,
									@PathVariable Long pageSize,
									@RequestBody TrackInfoQuery trackInfoQuery) {
		Page<TrackListVo> page = new Page<>(pageNo, pageSize);
		Long userId = AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		trackInfoService.findUserTrackPage(page, trackInfoQuery);
		return Result.ok(page);
	}

	@Operation(summary = "获取声音信息")
	@GetMapping("getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
		//	调用服务层方法
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	@PutMapping("/updateTrackInfo/{trackId}")
	@Operation(summary = "修改声音信息")
	public Result updateTrackInfo(@PathVariable Long trackId,
								  @RequestBody TrackInfoVo trackInfoVo) {

		trackInfoService.updateTrackInfo(trackInfoVo,trackId);
		return Result.ok();
	}

	@DeleteMapping("/removeTrackInfo/{trackId}")
	@Operation(summary = "删除声音信息")
	public Result removeTrackInfo(@PathVariable Long trackId) {
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	@GetMapping("/getTrackListById/{trackId}/{trackCount}")
	@Operation(summary = "根据声音id获取声音信息列表")
	@TsLogin
	Result<List<TrackInfo>> getTrackListById(@PathVariable Long trackId, @PathVariable Integer trackCount){
		Long userId = AuthContextHolder.getUserId();
		List<TrackInfo> list = trackInfoService.getTrackListById(trackId,trackCount,userId);
		return Result.ok(list);
	}
}

